package saxion.security.android;

import java.io.File;

import android.app.TabActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TabHost;

/**
 * Main activity
 * @author Saxion Security
 *
 */
public class SecurityAndroidActivity extends TabActivity {

	private String FILE = "File";
	
	public String createdEncryptedFolderLocation = "";
	public String createdDecryptedFolderLocation = "";

	//load strings from strings.xml
	private String tabSyncTitle;
	private String tabFilesTitle;
	private String tabSettingsTitle;
	
	//folders
	private String defaultDecryptedFolder;
	private String defaultEncryptedFolder;
	
	//preferences
	private SharedPreferences preferences;
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	
        super.onCreate(savedInstanceState);
        
        //gets the right strings from strings.xml      
        tabSyncTitle = getString(R.string.tab_sync_title);
        tabFilesTitle = getString(R.string.tab_files_title);
        tabSettingsTitle = getString(R.string.tab_settings_title);
        defaultEncryptedFolder = getString(R.string.default_encrypted_folder);
        defaultDecryptedFolder = getString(R.string.default_decrypted_folder);
        
        Resources res = getResources(); // Resource object to get Drawables
        TabHost tabHost = getTabHost();  // The activity TabHost
        TabHost.TabSpec spec;  // Resusable TabSpec for each tab
        Intent intent;  // Reusable Intent for each tab
       
        // Create an Intent to launch an Activity for the tab (to be reused)
        intent = new Intent().setClass(this, SynchroniseActivity.class);

        // Initialize a TabSpec for each tab and add it to the TabHost
        spec = tabHost.newTabSpec("sync").setIndicator(tabSyncTitle, res.getDrawable(R.drawable.ic_tab_synchronise)).setContent(intent);
        tabHost.addTab(spec);

        // Do the same for the other tabs
        intent = new Intent().setClass(this, FilesActivity.class);
        spec = tabHost.newTabSpec("files").setIndicator(tabFilesTitle, res.getDrawable(R.drawable.ic_tab_files)).setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, SettingsActivity.class);
        spec = tabHost.newTabSpec("Instellingen").setIndicator(tabSettingsTitle,res.getDrawable(R.drawable.ic_tab_preferences)).setContent(intent);
        tabHost.addTab(spec);

        tabHost.setCurrentTab(0);
        
        setup();
        
        super.onStart();
    	
    }
    
    
	private void setup() {
		//create a new folder where we store our encrypted files
		preferences = PreferenceManager.getDefaultSharedPreferences(this);
		
        String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
        createdEncryptedFolderLocation = extStorageDirectory + defaultEncryptedFolder;
        createdDecryptedFolderLocation = extStorageDirectory + defaultDecryptedFolder;
        
        //only create id if it the dir doesnt exsists
        File dir = new File(createdEncryptedFolderLocation);
        if(dir.exists() == false) {
            // if the dir does not exsist
            dir.mkdir();
            Log.v(FILE, "encrypted folder created");
        }

        File dir2 = new File(createdDecryptedFolderLocation);
        if(dir2.exists() == false) {
            // if the dir does not exsist
            dir2.mkdir();
            Log.v(FILE, "decrypted folder created");
        }
         

        //only change the string if it is empty
        if( preferences.getString("encryptedFolderLocation", "") == ""){
	        // edit the preferences
        	preferences.getString("encryptedFolderLocation", createdEncryptedFolderLocation);
	        SharedPreferences.Editor editor = preferences.edit();
	        editor.putString("encryptedFolderLocation", createdEncryptedFolderLocation);
	        editor.commit(); // Very important
        }
        //only change the string if it is empty
        if( preferences.getString("decryptedFolderLocation", "") == ""){
	        // edit the preferences
        	preferences.getString("decryptedFolderLocation", createdDecryptedFolderLocation);
	        SharedPreferences.Editor editor = preferences.edit();
	        editor.putString("decryptedFolderLocation", createdDecryptedFolderLocation);
	        editor.commit(); // Very important
        }
        
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
	 */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }
    
	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onOptionsItemSelected(android.view.Menu)
	 */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
        case R.id.afsluiten:
        	this.finish();
            return true;
        case R.id.credits:
        	Intent creditsIntent = new Intent(this, CreditsActivity.class);
        	startActivity(creditsIntent);
        	return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    } 
    
}